package com.soilflow.know.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.core.domain.entity.GamesInfo;
import com.soilflow.common.core.domain.entity.GamesScoreHistory;
import com.soilflow.common.core.domain.entity.GamesUserScore;
import com.soilflow.common.utils.bean.BeanUtils;
import com.soilflow.know.mapper.GamesInfoMapper;
import com.soilflow.know.mapper.GamesScoreHistoryMapper;
import com.soilflow.know.mapper.GamesUserScoreMapper;
import com.soilflow.know.service.GamesInfoService;
import com.soilflow.know.service.GamesUserScoreService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class GamesUserScoreServiceImpl extends ServiceImpl<GamesUserScoreMapper, GamesUserScore> implements GamesUserScoreService {


    @Resource
    private GamesScoreHistoryMapper gamesScoreHistoryMapper;

    @Override
    public GamesUserScore selectGamesUserScoreById(Long id) {
        GamesUserScore landInfoVos = this.baseMapper.selectGamesUserScoreById(id);
        return landInfoVos;
    }

    @Override
    public GamesUserScore selectGamesUserScoreByUserId(Long id) {
        GamesUserScore landInfoVos = this.baseMapper.selectGamesUserScoreByUserId(id);
        return landInfoVos;
    }


    @Override
    public List<GamesUserScore> selectGamesUserScoreList(GamesUserScore dto) {
        List<GamesUserScore> landInfoVos = this.baseMapper.selectGamesUserScoreList(dto);
        return landInfoVos;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertGamesUserScore(GamesUserScore dto) {
        boolean save =false;
        GamesUserScore landInfo = new GamesUserScore();
        BeanUtils.copyProperties(dto,landInfo);
        save = this.save(landInfo);
        return save;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addGamesUserScore(GamesUserScore dto) {
        GamesScoreHistory data = new GamesScoreHistory();
        data.setType("1");  // 上分
        data.setUserId(dto.getUserId());
        data.setOrigin("上分");
        data.setScoreTime(new Date());
        data.setScore(dto.getScore());
        data.setUserName(dto.getUserName());
        baseMapper.addGamesUserScore((dto));

        GamesUserScore gamesUserScore = selectGamesUserScoreByUserId(dto.getUserId());
        data.setUserScore(gamesUserScore.getScore());
        gamesScoreHistoryMapper.insert(data);
        return true;
    }

    @Override
    public Boolean addGamesUserScoreOnly(GamesUserScore dto) {
        baseMapper.addGamesUserScore((dto));
        return true;
    }

    @Override
    public Boolean delGamesUserScoreOnly(GamesUserScore dto) {
        Boolean res = baseMapper.delGamesUserScore((dto));
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delGamesUserScore(GamesUserScore dto) {
        GamesUserScore gamesUserScore = selectGamesUserScoreByUserId(dto.getUserId());

        GamesScoreHistory data = new GamesScoreHistory();
        data.setType("2");  // 上分
        data.setUserId(dto.getUserId());
        data.setOrigin("下分");
        data.setScoreTime(new Date());
        data.setScore(dto.getScore());
        data.setUserName(gamesUserScore.getUserName());

        Boolean res = baseMapper.delGamesUserScore((dto));
        data.setUserScore(gamesUserScore.getScore());
        gamesScoreHistoryMapper.insert(data);
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGamesUserScore(GamesUserScore dto) {
        boolean save =false;
        GamesUserScore landInfo = new GamesUserScore();
        BeanUtils.copyProperties(dto,landInfo);
        //修改百事通信息
        save = this.updateById(landInfo);
        return save;
    }

    @Override
    public Boolean deleteGamesUserScoreByIds(List<Long> ids) {
        return this.removeByIds(ids);
    }
}
